<template>
  <el-card shadow="never">
    <div slot="header" ref="topHeader">
      <el-form  :model="listQuery" label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input style="width: 100%" v-model="listQuery.name" @keypress.native.enter="getList" placeholder="输入姓名" clearable/>
            </el-form-item> 
          </el-col>
          <el-col :span="6">
            <el-form-item label="身份证号">
              <el-input style="width: 100%" v-model="listQuery.idcard" @keypress.native.enter="getList" placeholder="输入身份证号" clearable/>
            </el-form-item> 
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话">
              <el-input style="width: 100%" v-model="listQuery.phone" @keypress.native.enter="getList" placeholder="输入联系电话" clearable/>
            </el-form-item> 
          </el-col>
          <el-col :span="6">
            <el-form-item label="有无癌症史" >
              <el-select v-model="listQuery.isCancer" style="width: 100%" placeholder="选择有无癌症史" clearable>
                <el-option label="有" value="有" />
                <el-option label="无" value="无" />
              </el-select>
            </el-form-item> 
          </el-col>
          <el-col :span="6">
            <el-form-item label="卡号">
              <el-input style="width: 100%" v-model="listQuery.cardNum" @keypress.native.enter="getList" placeholder="输入卡号" clearable/>
            </el-form-item> 
          </el-col> 
          <!-- <el-col :span="8">
            <el-form-item label="激活时间:">
               <el-date-picker
                  v-model="listQuery.time1"
                   format="yyyy-MM-dd" style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
          </el-col> -->
          <el-col :span="12" style="margin-left: 10px;">
            <el-button type="primary" icon="el-icon-search" @click="getList" @keypress.native.enter="getList">查询</el-button>
            <el-button plain @click="resetHandle" >重置</el-button>
            <!-- <el-button type="success" v-permission="['sys:customer:add']" icon="el-icon-circle-plus-outline" @click="addEdit(null)">增加</el-button> -->
            <!-- <el-upload style="display: inline-block;margin-left:15px;"
              action=""
              :auto-upload="false"
              accept=".xlsx, .xls"
              :show-file-list="false"
              :on-change="handleUpload"
              name="file"
            >
              <el-button type="warning" size="small" :loading="importLoading" 
              v-permission="['sys:customer:import']">导入</el-button>
            </el-upload> -->

            <!-- <el-button type="success" size="small" :loading="importLoading"
              v-permission="['sys:customer:export']" @click="exportHandle" style="margin-left:15px;">导出</el-button> -->
          
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div v-loading="loading" >
      <el-table ref="tableCot" element-loading-text="请稍后..." :data="tableData"   >
        <el-table-column  type="index" width="50" label="序号"/>
        <el-table-column prop="name" label="姓名"  show-overflow-tooltip />
        <el-table-column prop="idcard" label="身份证号" width="180" show-overflow-tooltip />
        <el-table-column prop="phone" label="联系电话"  width="120" show-overflow-tooltip />
        <el-table-column prop="isCancer" label="有无癌症史"  width="100"  show-overflow-tooltip >
          <template slot-scope="{row}">
            <el-tag v-if="row.isCancer === 0">无</el-tag>
            <el-tag type="danger" v-if="row.isCancer === 1">有</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="age" label="年龄"  show-overflow-tooltip /> -->
        <el-table-column prop="cardNum" label="卡号" width="150"  show-overflow-tooltip />
        <el-table-column prop="activationTime" label="激活时间" width="150" show-overflow-tooltip />
        <el-table-column prop="startTime" label="生效时间"  width="150" show-overflow-tooltip />
        <el-table-column prop="endTime" label="到期时间"  width="150" show-overflow-tooltip /> 
        <!-- <el-table-column prop="remake" label="备注"  width="150" show-overflow-tooltip />  -->
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <!-- <el-tag v-permission="['sys:customer:update']" style="cursor:pointer;margin-right: 4px;" size="mini" @click="addEdit(scope.row)"><i
                class="el-icon-edit"/>编辑
            </el-tag> -->
            <el-tag style="cursor:pointer;margin-right: 4px;" size="mini" @click="handleView(scope.row)"><i
                class="el-icon-view"/>详情
            </el-tag>
            <el-tag v-permission="['sys:customer:delete']" style="cursor:pointer;" size="mini" type="danger" @click="remove(scope.row)"><i
                class="el-icon-delete"/>删除
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <!--分頁-->
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
      <!--添加/编辑-->
      <add-edit
          :dialog-visible="addEditVisible"
          :data="addEditData"
          @close="closeDialog"
          @reloadTable="reloadTable"
      />
      <!-- 详情弹窗 -->
      <CustDetail ref="custDetailRef" />
    </div>
  </el-card>
</template>
<script>
import Pagination from '@/components/Pagination'
import addEdit from '@/views/biz/customer/addEdit'
import CustDetail from '@/views/biz/customer/custDetail'
import customerApi from '@/api/system/customer'
import publisherApi from '@/api/system/publisher'

export default {
  name: 'customer',
  components: {Pagination, addEdit, CustDetail},
  data() {
    return {
      importLoading: false,
      loading: false,
      addEditVisible: false,
      customTableHeight: 200,
      total: 0,
      listQuery: {
        page: 1,
        pageSize: 10
      },
      showTable: true,
      tableData: [],
      addEditData: {},
      publisherList:[],
      curUserRole: -1,
    }
  },
  computed: {
    user() {
      return this.$store.state.user
    }
  },
  mounted() {
    this.user.roles.forEach(item => {
      if(item != 1 && item != 2){
        this.curUserRole = 3;
        if( this.user.publisherIds){
          this.listQuery.inPulisherId = this.user.publisherIds
        }else{
          this.listQuery.inPulisherId = ''
        }
      }else{
        this.listQuery.inPulisherId = ''
      }
    })
    
    this.getList();
    this.getPublisher()
  },
  methods: {
    getList() {
      this.loading = true;
      if(this.listQuery.time1){
        if(this.listQuery.time1.length > 0){
          this.listQuery.startTime = this.listQuery.time1[0];
          this.listQuery.endTime = this.listQuery.time1[1];
        }
      }
      customerApi.list(this.listQuery).then(res => {
         console.log(res,"1324")
        this.tableData = res.data || []
        this.total = res.count || 0
        this.loading = false;
      }).catch(() => this.loading = false)
    },
    handleView(row) {
      this.$refs.custDetailRef.showDialog(row)
    },
    resetHandle(){
      this.listQuery = {}
      this.getList();
    },
    getPublisher(){
      var params = {
        page: 1,
        pageSize: 1000,
      }
      publisherApi.getPublisher(params).then(res => {
        this.publisherList = res.data
      })
    },
    reloadTable(){
      this.getList();
    },
    //添加或修改弹窗
    addEdit(row) {
      if (row) {
        this.addEditData = JSON.parse(JSON.stringify(row))
      }else{
        this.addEditData = {};
      }
      this.addEditVisible = true
    },
    queryDetail(row){
      this.addEditData = JSON.parse(JSON.stringify(row))
      this.addEditVisible = true
    },
    remove(row) {
      this.$confirm('确定要删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        customerApi.remove(row.id).then(res => {
          this.reloadTable()
        })
      })
    },
    //导入
    handleUpload(data){
      this.importLoading = true;
      customerApi.importExcel(data.raw).then(res => {
        this.$message.success("导入成功！")
        this.importLoading = false;
        this.getList()
      })
    },
  //导出
    exportHandle(){
      this.importLoading = true; 
      customerApi.exportExcel(this.listQuery).then(res => {
        this.$utils.downloadFile(res.data,'客户管理','xlsx')
        this.importLoading = false;
        this.listQuery.size = 10;
      })
    },
    closeDialog() {
      this.addEditVisible = false
    }
  }
}
</script>
<style scoped lang="scss">

</style>
